package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UpdateOrgIdService extends BaseService {

    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;

    public RespEntity updateOrgId(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        ArrayList arrayList = new ArrayList();
        arrayList.add("device_alarm_log");
        arrayList.add("device_alarm_log_all_statistics_month");
        arrayList.add("device_alarm_log_all_statistics_week");
        arrayList.add("device_alarm_log_all_today");
        arrayList.add("device_alarm_log_electric_fire_statistics_month");
        arrayList.add("device_alarm_log_electric_fire_statistics_week");
        arrayList.add("device_alarm_log_electric_fire_today");
        arrayList.add("device_alarm_log_fire_water_statistics_month");
        arrayList.add("device_alarm_log_fire_water_statistics_week");
        arrayList.add("device_alarm_log_fire_water_today");
        arrayList.add("device_alarm_log_gas_statistics_month");
        arrayList.add("device_alarm_log_gas_statistics_week");
        arrayList.add("device_alarm_log_gas_today");
        arrayList.add("device_alarm_log_smoke_statistics_month");
        arrayList.add("device_alarm_log_smoke_statistics_week");
        arrayList.add("device_alarm_log_smoke_today");
        arrayList.add("device_alarm_log_visualization_statistics_month");
        arrayList.add("device_alarm_log_visualization_statistics_week");
        arrayList.add("device_alarm_log_visualization_today");
        arrayList.add("firelink_device.device_business");
        BulkUpdate(arrayList,jsonParam);
        return RespEntity.success();
    }

    public RespEntity BulkUpdate(List<String> tables, JSONObject jsonParam){
        for (String table : tables) {
            jsonParam.put("table", table);
            String orgId = jsonParam.getStr("orgId");
            deviceBusinessMapper.updateOrgId(jsonParam);
        }
        return RespEntity.success();
    }

    public RespEntity updateIotNumber(JSONObject jsonParam) {
        String number = jsonParam.getStr("number");
        String iotNumber = jsonParam.getStr("iotNumber");
        String ieimCode = jsonParam.getStr("ieimCode");
        if (number != null && number != "") {
            this.deviceBusinessMapper.updateIotNumber(jsonParam);
            return RespEntity.success();
        }
        return RespEntity.fail();
    }
}
